package vivek.project.pocketmoney.common.controls
{
	import flash.display.DisplayObject;
	import flash.events.Event;

	import mx.controls.Button;
	import mx.events.ValidationResultEvent;

	import spark.components.TextInput;

	import vivek.project.pocketmoney.common.valdiators.AdvancedStringValidator;

	public class TextInput2 extends spark.components.TextInput
	{

		public function TextInput2()
		{
			super();
		}
		private var _allowNumber:Boolean=false;
		private var _allowSpaces:Boolean=false;
		private var _allowSpecialChars:Boolean=false;
		private var _maxLen:int=20;
		private var _minLen:int=2;
		private var validator:AdvancedStringValidator=new AdvancedStringValidator();
		private var validatorEvent:ValidationResultEvent;

		public function get allowNumbers():Boolean
		{
			return _allowNumber;
		}

		public function set allowNumbers(allowNumber:Boolean):void
		{
			_allowNumber=allowNumber;
			validator.allowNumbers=_allowNumber;
		}

		public function get allowSpaces():Boolean
		{
			return _allowSpaces;
		}

		public function set allowSpaces(allowSpace:Boolean):void
		{
			_allowSpaces=allowSpace;
			validator.allowSpaces=allowSpace;
		}

		public function get allowSpecialChars():Boolean
		{
			return _allowSpecialChars;
		}

		public function set allowSpecialChars(allowSpecial:Boolean):void
		{
			_allowSpecialChars=allowSpecial;
			validator.allowSpecialCharacters=allowSpecial;
		}

		public function get maxLength():int
		{
			return _maxLen;
		}

		public function set maxLength(maxLen:int):void
		{
			_maxLen=maxLen;
			validator.maxCharacters=_maxLen;
		}

		public function get minLength():int
		{
			return _minLen;
		}

		public function set minLength(minLen:int):void
		{
			_minLen=minLen;
			validator.minCharacters=_minLen;
		}

		public function validateIt(event:Event):Boolean
		{
			//	text=formatter.format(text);
			var suppressEvents:Boolean=false;
			var currentTarget:DisplayObject=event.currentTarget as DisplayObject;
			var sourceTarget:DisplayObject=this;
			suppressEvents=!(sourceTarget == currentTarget);
			validatorEvent=validator.validateString(text);
			if (validatorEvent.type == ValidationResultEvent.VALID)
			{
				this.errorString="";
			}
			else
			{
				if (!suppressEvents)
				{
					this.errorString=validatorEvent.message;
					/*var button:Button=new Button();
					   button.label="E";
					   button.x=this.x + 5;
					   button.y=this.y + 5;
					 addChild(button);*/
				}
			}
			return (validatorEvent.type == ValidationResultEvent.VALID)
		}

		override protected function createChildren():void
		{
			super.createChildren();
			height=29;
		}

		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			//textDisplay.=5;
		}
	}
}